﻿using System.ComponentModel;
using System.Reflection;
using System.Linq;

namespace HugeFlow.MVVM
{
    /// <summary>
    /// 휴즈플로우 MVVM 패턴의 뷰모델 추상클래스
    /// </summary>
    public abstract class ViewModelBase : INotifyPropertyChanged
    {

        #region 이벤트들

        /// <summary>
        /// 프로퍼티가 변경되었음을 알려주는 이벤트
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(params string[] properties)
        {
            if (PropertyChanged != null)
            {
                foreach (string property in properties)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }
        }

        #endregion 이벤트들

        #region 메서드들

        /// <summary>
        /// 종속성을 해제합니다.
        /// </summary>
        public virtual void Clean()
        {
            PropertyChanged = null;
        }

        /// <summary>
        /// 바인딩을 새로고침합니다.
        /// </summary>
        public void RefreshBindings()
        {
            var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var names = (from p in properties select p.Name).ToArray();
            OnPropertyChanged(names);
        }

        #endregion 메서드들
    }
}
